package com.sl.boot.admindemo.domain.role.user.controller;


import com.sl.boot.admindemo.commmon.utils.RedisUtil;
import com.sl.boot.admindemo.dao.PwdUserDAO;
import com.sl.boot.admindemo.domain.role.user.entity.PwdUser;
import com.sl.boot.admindemo.domain.role.user.entity.PwdUserExample;
import com.sl.boot.admindemo.commmon.service.EmailService;
import com.sl.boot.admindemo.domain.role.user.utils.VerifyCodeUtils;
import com.sl.boot.admindemo.domain.role.user.vo.LoginVo;
import com.sl.boot.admindemo.commmon.vo.resp.LoginResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@RestController
public class LoginController {

    @Autowired
    private PwdUserDAO pwdUserDAO;

    @Resource
    RedisUtil redisUtil;

    @Resource
    EmailService emailUtil;

    private final String prefix = "email:";

    @PostMapping("/login")
    public LoginResp login(@RequestBody LoginVo loginVo, HttpSession session) {

        //图形验证码
        String sessionCode = session.getAttribute("code").toString();
        if(!sessionCode.equals(loginVo.getCode())) 
            return new LoginResp(-1, "图形验证码错误");
        
        LoginResp loginResp = new LoginResp();
        PwdUserExample pwdUserExample = new PwdUserExample();
        
        pwdUserExample.createCriteria().andUserNameEqualTo(loginVo.getUsername()).andPasswordEqualTo(loginVo.getPassword());
        List<PwdUser> pwdUsers = pwdUserDAO.selectByExample(pwdUserExample);
        if (pwdUsers.isEmpty()) {
            return new LoginResp(404, "用户名或密码错误");
        }

        PwdUser pwdUser = pwdUsers.get(0);
        if (!loginVo.getRole().equals(pwdUser.getUserType().toString())) {
            return new LoginResp(424, "用户名或密码错误");
        }
        if (loginVo.getPassword().equals(pwdUser.getPassword())) {
            loginResp.setType(pwdUser.getUserType());
        }
        return loginResp;

    }

    /**
     * 获取邮箱验证码
     *
     * @param email 邮箱号
     */
    @RequestMapping("/getEmailCode/{email}")
    public LoginResp getEmailCode(@PathVariable String email) {

        /*
         * （1）生成验证码
         * （2）验证码存Redis(5分钟过期)
         * （3）发送邮件
         * （4）查询Redis验证
         */
        String randomCode = emailUtil.randomCode();
        emailUtil.sendMimeMail(email, randomCode);
        redisUtil.set(prefix+email, randomCode, 5*60);

        return new LoginResp();
    }
    
    /**
     * 生成图形验证码
     */
    @RequestMapping("/user/generateImageCode")
    public void generateImageCode(HttpSession session, HttpServletResponse response) throws IOException {
        //1.生成4位随机数
        String code = VerifyCodeUtils.generateVerifyCode(4);
        //2.保存到session作用域
        session.setAttribute("code",code);
        //3.根据随机数生成图片 && 4.通过response响应图片  && 5.设置响应类型
        response.setContentType("image/png");
        ServletOutputStream os = response.getOutputStream();
        VerifyCodeUtils.outputImage(220,60, os,code);
    }
}